<template>
  <el-card header="数据备份">
    <div class="tree-box">
      <MForm :fields="fields" @submit="search" @cancel="search">
        <template #extra>
          <!--<el-button type="primary" @click="setTheBackupCycle"-->
          <!--  >设定备份周期</el-button-->
          <!--&gt;-->
          <el-button type="primary" @click="manualBackups" :loading="loading">{{
            loading ? '手动备份中' : '手动备份'
          }}</el-button>
        </template>
      </MForm>
      <MTable
        :columns="columns"
        :http="backupPage"
        :params="params"
        v-model:isReload="isReload"
        v-model:selectKeys="selectKeys"
        rowKey="sysBackupLogId"
        height="calc(100vh - 340px)"
      >
        <template #actions="{ row }">
          <MA
            type="primary"
            @click="download(row.sysBackupLogId, row.backupName)"
            >下载</MA
          >
        </template>
      </MTable>
    </div>
  </el-card>
  <el-dialog title="下载" width="428px" v-model="dialogVisible">
    <div class="dialog-row">
      <div class="dialog-col">文件名称：{{ fileInfo.name }}</div>
      <div class="dialog-col">提取密码：{{ fileInfo.pwd }}</div>
    </div>
    <div class="btns">
      <el-button type="primary" @click="submit" class="ml-20" :loading="loading"
        >确定</el-button
      >
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
  <MFormDialog
    v-model="formDialog.visible"
    v-bind="formDialog"
    :fields="dialogFields"
    :httpAdd="backupChange"
    @reload="reload"
    :handler="handler"
  ></MFormDialog>
  <el-dialog title="手动备份" width="428px" v-model="dialogVisibleBackup">
    <el-progress :percentage="50" />
    <div style="padding: 20px 0 20px">正在备份，已完成50%</div>
    <div class="btns">
      <el-button @click="dialogVisible = false">取消备份</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { columns, fields, dialogFields } from './config'
import { wrapperLoadViteEnv } from '~/build/utils'
const viteEnv = wrapperLoadViteEnv(import.meta.env)
import {
  backupDownload,
  backupPage,
  backupChange,
  backup
} from '@/api/system/globalSettings'
import { ElMessage } from 'element-plus'
import db from '@/storage'
import { useUserStore } from '@/store'

const userStore = useUserStore()

defineOptions({
  name: 'DataQueryThreshold'
})

// 搜索
const params = ref<any>({})
const search = (data: any) => {
  params.value = {
    ...params.value,
    ...data
  }
}

// 删除
const selectKeys = ref<number[]>([])
const clearSelectKeysReload = () => {
  selectKeys.value = []
  reload()
}

// 重载
const isReload = ref(true)
const reload = () => {
  isReload.value = true
}

const openDialog = () => {
  dialogVisible.value = true
}

const options = ref([
  {
    label: '主题1',
    value: 1
  },
  {
    label: '主题2',
    value: 2
  },
  {
    label: '主题3',
    value: 3
  }
])

// 新增/编辑
const dialogVisible = ref(false)

const fileInfo = ref<Record<string, any>>({})
const download = (id, name) => {
  backupDownload(id).then((res) => {
    fileInfo.value.id = id
    fileInfo.value.pwd = res.data || ''
    fileInfo.value.name = name
    dialogVisible.value = true
  })
}

const submit = () => {
  //const token = db.get('token')
  window.open(
    viteEnv.VITE_BASE_URL_API +
      `hbsys-system/backup/download/${fileInfo.value.id}/${userStore?.userInfo?.userId}`
  )
  dialogVisible.value = false
}

//设定备份周期
const formDialog = ref({
  visible: false,
  id: 0
})

const handler = (e) => {
  let d = JSON.parse(JSON.stringify(e))
  if (d?.time) {
    d.hours = Number(d.time.split(':')[0])
    d.minute = Number(d.time.split(':')[1])
    delete d.time
  }
  return d
}

const setTheBackupCycle = () => {
  formDialog.value.visible = true
}

//手动备份
const dialogVisibleBackup = ref(false)
const loading = ref(false)
const manualBackups = () => {
  loading.value = true
  backup().finally(() => {
    loading.value = false
  })
}
</script>
<style lang="scss" scoped>
.tree-box {
  position: relative;

  .nodeBox {
    width: 100%;
    height: 80vh;
    overflow-x: auto;
    cursor: all-scroll;
  }
  .showLevel {
    position: absolute;
    right: 0;
    top: 0;
  }
}
.dialog-row {
  width: 100%;
  .dialog-col {
    width: 100%;
    display: flex;
    .dialog-col-serialNumber {
      color: #333;
      height: 100%;
      display: flex;
      justify-content: center;
      align-content: center;
      font-weight: bold;
      margin-right: 20px;
    }
    .dialog-col-rig {
      flex: 1;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid rgba(237, 237, 237, 1);
      margin-bottom: 20px;
      padding-bottom: 20px;
      .dialog-col-rig-content {
        .dialog-col-rig-content-title {
          font-size: 13px;
          color: #666;
          margin-bottom: 20px;
        }
        .dialog-col-rig-content-form {
          font-size: 13px;
          color: #999;
        }
      }
    }
  }
}
.btns {
  display: flex;
  flex-direction: row-reverse;
}
.ml-20 {
  margin-left: 20px;
}
.ml-12 {
  margin-left: 12px;
}
.dialog-row {
  width: 100%;
  margin-bottom: 20px;
  .dialog-col {
    width: 100%;
    font-size: 14px;
    color: #000;
    margin-bottom: 20px;
  }
}
</style>
